/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package examplesocket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.spec.SecretKeySpec;

/**
 *
 * @author giace
 */
public class ServerReaderBytes extends Thread{
    Socket client;
    Boolean stillRunning;
    Stream thisStream;
    InputStream lettore;
    ServerReaderBytes(Socket pServer, Stream s)
    {
        try 
        {
            client = pServer;
            lettore = client.getInputStream();
            thisStream = s;
            stillRunning=true;
        }
        catch (Exception ex)
        {
            System.err.append(ex.getMessage());
        }
    }
    
    public void serviceStop()
    {
        stillRunning = false;
    }
    
    @Override
    public void run()
    {
        try {
            while( lettore.available() == 0);
            int len = lettore.available();
            byte key[] = new byte[len];
            for(int i=0;i<len;i++)
                key[i]= (byte) lettore.read();
            SecretKeySpec sks = new SecretKeySpec(key,"AES");
            thisStream.getCurrentUser().setAESKey(sks);
            System.out.println("Length " + sks.getEncoded().length + " Chiave: " + sks.toString());
            thisStream.changeStreamReader();
        }catch(IOException ioe) {
            System.err.println(ioe.getMessage());
        }
    }
}
